package com.gzdh.dishrecognition.ui.adapter.defaultview;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.ProductConst;
import com.gzdh.dishrecognition.entity.BmzlEntity;
import com.gzdh.dishrecognition.entity.WeekMenuBakEntity;
import com.gzdh.dishrecognition.model.ProductCartModel;
import com.gzdh.dishrecognition.model.WeekMenuBakModel;
import com.gzdh.dishrecognition.ui.view.HorizontalListView;

import java.util.List;
import java.util.stream.Collectors;

public class BmzlHorizontalAdapter extends BaseAdapter {

    private Context mContext;


    private List<WeekMenuBakModel> list;

    private int select = -1;


    public BmzlHorizontalAdapter(Context context) {
        mContext = context;
    }


    public void setList(List<WeekMenuBakModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.view_string_gridview_item2, null);
            holder = new ViewHolder();
            holder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
            holder.mainLinear = (LinearLayout) convertView.findViewById(R.id.mainLinear);
            holder.itemBadge = (TextView) convertView.findViewById(R.id.itemBadge);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nameTv.setText(list.get(position).getName().trim());
        if (position == select) {
            holder.mainLinear.setBackgroundColor(Color.rgb(200, 200, 200));
        } else {
            holder.mainLinear.setBackgroundColor(Color.rgb(255, 255, 255));
        }

        List<ProductCartModel> productCartModelList = ProductConst.getInstance().getProductCartModelList().stream()
                .filter(productCartModel -> productCartModel.getDinner().equals(list.get(position).getBmcm()))
                .filter(productCartModel -> productCartModel.getZl().equals(list.get(position).getBm()))
                .collect(Collectors.toList());
        holder.itemBadge.setVisibility(View.INVISIBLE);
        if (productCartModelList.size() > 0) {
            holder.itemBadge.setVisibility(View.VISIBLE);

            int counts = 0;
            for (ProductCartModel goo : productCartModelList) {
                counts += goo.getNumber();
            }
            holder.itemBadge.setText(String.valueOf(counts));
        }

        return convertView;
    }

    static class ViewHolder {
        public TextView nameTv;
        private LinearLayout mainLinear;
        private TextView itemBadge;
    }


    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }
}
